package com.chen.controller;

import com.chen.enums.Enums;
import com.chen.pojo.User;
import com.chen.pojo.beans.CommonResult;
import com.chen.pojo.model.LoginForm;
import com.chen.service.Impl.LoginServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController  //标注控制层组件，将函数的返回值直 接填入HTTP响应体中
@RequestMapping(value = "/sys/api/user") //用来映射请求
@Api(value = "UserController", tags = {"用户接口"})
public class LoginController {

    private static Logger log = LoggerFactory.getLogger(LoginController.class);
    @Resource //自动装配
    private HttpServletRequest request;
    @Resource
    private LoginServiceImpl loginService;

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody //返回结果直接写入 HTTP response body 中
    @ApiOperation(value = "登录")
    public CommonResult userLogin(@RequestBody LoginForm loginForm) {//接收的是请求体里面的数据
        return loginService.login(loginForm);

    }

    @RequestMapping(value = "/register", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "注册")
    public CommonResult userRegister(@RequestBody User user) {
        if (loginService.insert(user) == 1) {
            return new CommonResult(Enums.SUCCESS, "");
        }
        return new CommonResult(Enums.SUCCESS_USER_RELOGIN_REGISTER, "");


    }

    @RequestMapping(value = "/changeUser", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "修改")
    public CommonResult userUpdate(@RequestBody User user) {
        if (loginService.updateByPrimaryKey(user) == 1) {
            return new CommonResult(Enums.SUCCESS, "");
        }
        return new CommonResult(Enums.FAILED_UPDATE_USER, "");


    }


    @RequestMapping(value = "/info/{t}", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "用户基础信息")
    public CommonResult getInfo(@PathVariable String t) { //绑定它传过来的值到方法的参数上
        log.info("UserController-info: timestamp = " + t);
        String token = "";
        Long originalKeepAlive = null;
        Long timestamp = null;//时间戳
        try {
            timestamp = Long.parseLong(t);//是将参数转换成long的包装类
            token = request.getHeader("token");
            originalKeepAlive = Long.valueOf(request.getHeader("keepAlive"));
        } catch (Exception e) {
            log.error("[info] get info is error {}", e.getMessage());
        }
        log.info("UserController-info: token = " + token);
        log.info("UserController-info: originalKeepAlive = " + originalKeepAlive);
        return loginService.info(timestamp, token, originalKeepAlive);

    }

    @ResponseBody
    @RequestMapping(value = "/logout{t}", method = RequestMethod.POST)
    @ApiOperation("用户退出")
    public CommonResult getLogOut(@PathVariable String t) {
        String token = "";
        Long originalKeepAlive = null;
        try {
            token = request.getHeader("token");
            originalKeepAlive = Long.valueOf(request.getHeader("keepAlive"));
        } catch (Exception e) {
            log.error("The Request Headers or params is null.");
        }
        log.info("UserController-info: token = " + token);
        log.info("UserController-info: originalKeepAlive = " + originalKeepAlive);
        return loginService.logout(token, originalKeepAlive);

    }
}
